package com.piggy.logi.sys.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piggy.common.core.exception.GlobalException;
import com.piggy.logi.sys.bo.LogiWaybillLoadEditBo;
import com.piggy.logi.sys.domain.LogiWaybillLoad;
import com.piggy.logi.sys.mapper.LogiWaybillLoadMapper;
import com.piggy.logi.sys.service.ILogiWaybillLoadService;
import com.piggy.logi.sys.service.ILogiWaybillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 运单装车Service业务层处理
 */
@Slf4j
@Service
public class LogiWaybillLoadServiceImpl extends ServiceImpl<LogiWaybillLoadMapper, LogiWaybillLoad> implements ILogiWaybillLoadService {

    @Resource
    private ILogiWaybillService logiWaybillService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void load(LogiWaybillLoadEditBo editBo) {
        if (CollUtil.isEmpty(editBo.getWaybillIds())) {
            throw new GlobalException("装车运单不能为空");
        }
        LogiWaybillLoad entity = BeanUtil.toBean(editBo, LogiWaybillLoad.class);
        this.save(entity);
        logiWaybillService.load(editBo.getWaybillIds(), entity.getLoadId(), entity.getType());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelLoad(Integer type, Long loadId) {
        this.removeById(loadId);
        logiWaybillService.cancelLoad(loadId, type);
    }
}
